package com.maisha.Controller.cms;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.maisha.Service.IService.ICmsLoginService;
import com.maisha.base.Controller.BaseController;
import com.maisha.bean.Auditor;
import com.maisha.form.LoginForm;
import com.maisha.utils.CMSConstant;
import com.maisha.utils.CookieUtils;
import com.maisha.utils.MD5Utils;


@Controller(value = "cmsLoginController")
@RequestMapping(value="cms")
public class CmsLoginController extends BaseController {
	
	@Resource
	private ICmsLoginService cmsLoginService; //引入loginService
	
	
	/** cms  index page
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="index")
	public ModelAndView cmsIndex(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		//判断session 是否过期 session 不过期 跳转到 管理页
		Auditor auditor = (Auditor) request.getSession().getAttribute(CMSConstant.SESSION_AUDITOR_KEY);
		if(auditor!=null){
			mv.setViewName("jsp/cms/index");
		}else{
			//读取cookie
			String userAndPass = CookieUtils.getCookie(request);
			if(StringUtils.isBlank(userAndPass)){
				mv.setViewName("jsp/cms/input");
			}else{
				//获取用户信息 存session
				String[] userInfo = userAndPass.split(" ");
				auditor = cmsLoginService.getObjectAuditor(userInfo[0],userInfo[1]);
				request.getSession().setAttribute(CMSConstant.SESSION_AUDITOR_KEY,auditor);
				//获取用户权限存session
				
				
				mv.setViewName("jsp/cms/index");
			}
		}
		
		return mv;
	}
	
	/** 到打登录界面
	 * @param request
	 * @param response
	 * @param loginForm
	 */
	@RequestMapping(value="loginIn")
	public ModelAndView loginIn(HttpServletRequest request, HttpServletResponse response,LoginForm loginForm){
		ModelAndView mv = new ModelAndView("jsp/cms/input");
		String name = loginForm.getUsername();
		String password = loginForm.getPassword();
		Auditor auditor = null;
		
		if(request.getSession().getAttribute(CMSConstant.SESSION_AUDITOR_KEY)!=null){
			mv.setViewName("jsp/cms/index");
		}else{
			if(StringUtils.isNotBlank(name) && StringUtils.isNotBlank(password)){
				auditor = cmsLoginService.getObjectAuditor(name,MD5Utils.getMD5Result(password));
			}
			if(auditor!=null){
				//读取cookie
				String content = auditor.getAuditName() +" "+MD5Utils.getMD5Result(password);
				if("on".equals(loginForm.getIsRemember())){
					CookieUtils.setCookie(response, content, CMSConstant.COOKIE_ALIVE_TIME);
				}
				request.getSession().setAttribute(CMSConstant.SESSION_AUDITOR_KEY,auditor);
				//获得登录在的角色 权限 放入session 
				
				
				
				mv.setViewName("jsp/cms/index");
			}
		}
		return mv;
	}
	
	/** 用户退出 清除cookie  关闭session 跳转到登录界面
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="loginOut")
	public ModelAndView loginOut(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		//读取cookie
		request.getSession().setAttribute(CMSConstant.SESSION_AUDITOR_KEY,null);
		CookieUtils.deleteCookie(response, CMSConstant.COOKIE_AUDITOR_KEY);
		mv.setViewName("jsp/cms/input");
		return mv;
	}
	
	
	/** 获得 导航树
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="tree")
	public ModelAndView tree(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("jsp/cms/tree");
		return mv;
	}
	
	/** 获得
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="top")
	public ModelAndView topPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("jsp/cms/top");
		return mv;
	}
	
	/** 获得cms 的主页 信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="rightIndex")
	public ModelAndView rightIndex(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("jsp/cms/rightIndex");
		return mv;
	}
	
	
	/** 用户退出 清除cookie  关闭session 跳转到登录界面
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="loginOut2")
	public void loginOut2(HttpServletRequest request, HttpServletResponse response){
		//读取cookie
		request.getSession().setAttribute(CMSConstant.SESSION_AUDITOR_KEY,null);
		CookieUtils.deleteCookie(response, CMSConstant.COOKIE_AUDITOR_KEY);
	}
	
}
